package cn.pconline.autoclub.util.ubb;

import java.util.regex.Pattern;

/**
 *
 * @author xhchen
 */
public abstract class EscapeUtils {

	public static void appendHtml(CharSequence add, StringBuilder to) {
		if (add == null) {
			return;
		}

        //判断是否是审核
//        boolean isCensor = EnvUtils.getEnv().getObjectFromCache("_CENSOR_") != null;

        boolean isCensor = false; //这里先写死，需要重构

		for (int i = 0, c = add.length(); i < c; ++i) {
			char ch = add.charAt(i);
			switch (ch) {
				case ' ':
                    if (isCensor) {//如果是审核，则把空格替换成6个空格(等于&nbsp;的长度)
                        to.append("      ");
                    } else {
                        to.append("&nbsp;");
                    }
					break;
				case '<':
					to.append("&lt;");
					break;
				case '>':
					to.append("&gt;");
					break;
				case '\n':
					to.append("<br/>");
					break;
				case '\r':
					// ignore
					break;
				default:
					to.append(ch);
					break;
			}
		}
	}

	public static void appendAttribute(CharSequence add, StringBuilder to) {
		if (add == null) {
			return;
		}

		for (int i = 0, c = add.length(); i < c; ++i) {
			char ch = add.charAt(i);
			switch (ch) {
				case '"':
					to.append("&quot;");
					break;
				case '\n':
					to.append("<br/>");
					break;
				case '\r':
					// ignore
					break;
				default:
					to.append(ch);
					break;
			}
		}

	}

	static Pattern internalLinkPattern = Pattern.compile(
				"^\\s*http\\://[^.]+\\.(?:pconline\\.com\\.cn|pcgames\\.com\\.cn|pcauto\\.com\\.cn|pclady\\.com\\.cn|pcbaby\\.com\\.cn|pchouse\\.com\\.cn)(\\:|\\/).*$",
				Pattern.CASE_INSENSITIVE);
	public static boolean isInternalLink(CharSequence link) {
		return internalLinkPattern.matcher(link).find();
	}
}
